//
//  YBQueuePopupVC.swift
//  Moonlight
//
//  Created by Penlon Kim on 2024/1/5.
//  Copyright © 2024 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit
import SwiftyJSON
import HandyJSON

class YBQueuePopupVC: BaseMaskVC {
    
    deinit {
        gcdTimer.cancel()
    }
    
    private let type: Int
    private let queueNum: Int
    
    init(type: Int, queueNum: Int) {
        self.type = type
        self.queueNum = queueNum
        super.init(nibName: nil, bundle: nil)
        desLabel.text = "前方排队人数\(queueNum)人\n预计等待大约\(convertMinutesToHoursAndMinutes(20*queueNum))"
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(bgImgView)
        bgImgView.addSubviews([titleLabel, closeBtn, queueImgView, desLabel])
        
        bgImgView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(kScreenWidth*0.693)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(16).priority(.high)
            make.height.equalTo(18)
        }
        
        closeBtn.snp.makeConstraints { make in
            make.size.equalTo(15)
            make.right.equalTo(-10)
            make.top.equalTo(12)
        }
        
        queueImgView.snp.makeConstraints { make in
            make.size.equalTo(48)
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(21)
        }
        
        desLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(queueImgView.snp.bottom).offset(22)
            make.bottom.equalTo(-25)
        }
        
        gcdTimer.start()
        
    }
    
    func convertMinutesToHoursAndMinutes(_ totalMinutes: Int) -> String {
        if totalMinutes <= 60 {
            return "\(totalMinutes)min"
        } else {
            let hours = totalMinutes / 60
            let minutes = totalMinutes % 60
            return "\(hours)hour\(minutes)min"
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
    }
    
    // MARK: - lazy
    lazy var vm: YBHomeViewModel = {
        let vm = YBHomeViewModel()
        return vm
    }()
    
    private lazy var gcdTimer: YBGCDTimer = {
        let gcd = YBGCDTimer(interval: 5.0, action: { [weak self] _ in
            guard let self = self else { return }
            self.get_GetQueueNum()
        })
        return gcd
    }()
    
    private lazy var bgImgView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "icon_popup_bg"))
        iv.isUserInteractionEnabled = true
        iv.bk_addCornerBorder(radius: 8, borderWidth: 0.1, borderColor: .clear)
        iv.contentMode = .scaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
    
    private lazy var closeBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "icon_popup_close"), for: .normal)
        btn.bk_addTarget { [weak self] sender in
            self?.get_CutQueueNum()
        }
        return btn
    }()
    
    private lazy var titleLabel = self.bk_addLabel(text: "排队", font: .systemFont(ofSize: 16, weight: .medium), bgColor: .clear, textColor: ColorDef.dark27, align: .center)
    
    private lazy var queueImgView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "icon_popup_queue"))
        iv.contentMode = .scaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
    
    private lazy var desLabel = self.bk_addLabel(text: "前方排队人数3人\n预计等待大约50min", font: .systemFont(ofSize: 14), bgColor: .clear, textColor: ColorDef.gray153, align: .center)
    
}

// MARK: - 网络请求
extension YBQueuePopupVC {
    
    fileprivate func get_GetQueueNum() {
        vm.req_GetQueueNum(type.string) { [weak self] ok, obj, msg, code in
            if ok {
                guard let self = self,
                      let js = obj as? JSON,
                      let data = js[NetDataKey].int else { return }
                if data > 0 { // 排队
                    self.desLabel.text = "前方排队人数\(data)人\n预计等待大约\(convertMinutesToHoursAndMinutes(20*data))"
                } else {
                    self.dismiss(animated: true)
                }
            } else {
                HintTool.toast(msg)
            }
        }
    }
    
    fileprivate func get_CutQueueNum() {
        vm.req_CutQueueNum(type.string) { [weak self] ok, obj, msg, code in
            if ok {
                self?.dismiss(animated: true)
            } else {
                self?.dismiss(animated: true)
                HintTool.toast(msg)
            }
        }
    }
    
}
